<?php
namespace app\api\service\ceping;

// ====================== 核心算法 ======================
class XuexinengliService {
    private $dimensionWeights = [
        '信息获取' => 1.1,
        '知识加工' => 1.3,
        '记忆保持' => 0.9,
        '迁移应用' => 1.2,
        '元认知' => 1.15,
        '学习效能' => 1.0
    ];
	//采用李克特5级量表（1=完全不符合，5=完全符合）：
	// ====================== 题库配置 ======================
	public $questionBank = [
		'信息获取' => [
			['id' => 1, 'text' => '我能快速抓住学习材料的核心观点', 'type' => '正向', 'weight' => 1.1],
			['id' => 2, 'text' => '阅读专业文献时经常感觉找不到重点', 'type' => '反向', 'weight' => 1.0],
			['id' => 3, 'text' => '听课时能同步整理出清晰的逻辑框架', 'type' => '正向', 'weight' => 1.2]
		],
		'知识加工' => [
			['id' => 4, 'text' => '我常将新知识与已有经验建立联系', 'type' => '正向', 'weight' => 1.3],
			['id' => 5, 'text' => '遇到复杂概念时能自行拆解分析', 'type' => '正向', 'weight' => 1.2],
			['id' => 6, 'text' => '公式推导过程对我来说难以理解', 'type' => '反向', 'weight' => 1.1]
		],
		'记忆保持' => [
			['id' => 7, 'text' => '学过的知识在1个月后仍能清晰回忆', 'type' => '正向', 'weight' => 1.1],
			['id' => 8, 'text' => '我使用记忆宫殿等科学记忆方法', 'type' => '正向', 'weight' => 1.2],
			['id' => 9, 'text' => '我会合理分配不同科目的复习时间', 'type' => '反向', 'weight' => 1.2]
		],
		'迁移应用' => [
			['id' => 10, 'text' => '我能将A学科的方法应用到B学科问题', 'type' => '正向', 'weight' => 1.1],
			['id' => 11, 'text' => '遇到实际问题时常不知如何运用理论知识', 'type' => '反向', 'weight' => 1.2],
			['id' => 12, 'text' => '在小组讨论中经常提出创新解决方案', 'type' => '正向', 'weight' => 1.2]
		],
		'元认知' => [
			['id' => 13, 'text' => '学习前会明确制定目标和效果检验标准', 'type' => '正向', 'weight' => 1.1],
			['id' => 14, 'text' => '定期反思调整自己的学习方法', 'type' => '正向', 'weight' => 1.2],
			['id' => 15, 'text' => '经常学了很久才发现方向错误', 'type' => '反向', 'weight' => 1.2]
		],
		'学习效能' => [
			['id' => 16, 'text' => '每天专注学习时间可达4小时以上', 'type' => '正向', 'weight' => 1.1],
			['id' => 17, 'text' => '相同内容我的掌握速度比多数同学快', 'type' => '正向', 'weight' => 1.2],
			['id' => 18, 'text' => '经常出现学完就忘的情况', 'type' => '反向', 'weight' => 1.2]
		]
	];
    // 生成测评试卷
    public function generateTest($numPerDimension = 3) {
        $test = [];
        foreach ($this->questionBank  as $dim => $questions) {
            shuffle($questions);
            $test = array_merge($test, array_slice($this->questionBank[$dim], 0, $numPerDimension));
        }
        return $test;
    }

    // 计算得分
    // 计算能力得分
    public function calculateScore($answers) {
        $dimensionScores = [];
        $totalWeighted = 0;
        $maxPossible = 0;

        foreach ($this->questionBank as $dim => $questions) {
            $dimScore = 0;
            foreach ($questions as $q) {
                if (!isset($answers[$q['id']])) continue;
                
                // 处理反向题
                $effectiveScore = ($q['type'] == '反向') ? 
                    (6 - $answers[$q['id']]) : 
                    $answers[$q['id']];
                
                // 计算加权分
                $dimScore += $effectiveScore * $q['weight'];
            }
            // 标准化到0-100
            $maxDimScore = 5 * count($questions) * $q['weight'];
            $standardScore = ($dimScore / $maxDimScore) * 100;
            
            $dimensionScores[$dim] = round($standardScore, 1);
            $totalWeighted += $standardScore * $this->dimensionWeights[$dim];
            $maxPossible += 100 * $this->dimensionWeights[$dim];
        }

        // 计算综合指数
        $abilityIndex = round(($totalWeighted / $maxPossible) * 100, 1);
        
	
		//建议
		$suggestion = [];
		$suggestions = $this->generateSuggestions($dimensionScores);	
		foreach ($suggestions as $dim =>$advice) {
			$suggestion["{$dim}"]= $advice;
		}
        return [
            'score' => $abilityIndex,
            'level' => $this->getLevel($abilityIndex),
            'suggestion' => $suggestion,
            'dimensions' => $dimensionScores,
        ];
    }

    private function getLevel($score) {
		if ($score >= 85) return '卓越-具备自主学习能力，善于知识迁移';
        if ($score >= 70) return '优秀-系统化学习能力强，局部可优化';
        if ($score >= 55) return '合格-基础能力达标，需方法升级';
        return '待提升-需要系统性训练学习策略';
    }
	// 生成改进建议
    public function generateSuggestions($scores) {
        $advice = [];
        foreach ($scores as $dim => $score) {
            if ($score < 60) {
                $advice[$dim] = $this->getDimensionAdvice($dim);
            }
        }
        return ($advice);
    }

    private function getDimensionAdvice($dim) {
        $adviceMap = [
            '信息获取' => '使用SQ3R阅读法提升信息抓取能力',
            '知识加工' => '进行概念图练习，每周完成3个知识模块的思维导图',
            '元认知'   => '实施PDCA学习循环，每天记录学习日志'
        ];
        return $adviceMap[$dim] ?? '加强该领域的专项训练';
    }
}

?>